{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f986c837",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_csv('shanghai.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f91132a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>prov_code</th>\n",
       "      <th>city_code</th>\n",
       "      <th>region_code</th>\n",
       "      <th>prov_name</th>\n",
       "      <th>city_name</th>\n",
       "      <th>region_name</th>\n",
       "      <th>date</th>\n",
       "      <th>statis_type</th>\n",
       "      <th>value</th>\n",
       "      <th>statis_field</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>310000</td>\n",
       "      <td>310100</td>\n",
       "      <td>310101</td>\n",
       "      <td>上海市</td>\n",
       "      <td>上海市</td>\n",
       "      <td>黄浦区</td>\n",
       "      <td>20230331</td>\n",
       "      <td>App</td>\n",
       "      <td>852</td>\n",
       "      <td>天猫</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>310000</td>\n",
       "      <td>310100</td>\n",
       "      <td>310101</td>\n",
       "      <td>上海市</td>\n",
       "      <td>上海市</td>\n",
       "      <td>黄浦区</td>\n",
       "      <td>20230331</td>\n",
       "      <td>App</td>\n",
       "      <td>18016</td>\n",
       "      <td>拼多多</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>310000</td>\n",
       "      <td>310100</td>\n",
       "      <td>310101</td>\n",
       "      <td>上海市</td>\n",
       "      <td>上海市</td>\n",
       "      <td>黄浦区</td>\n",
       "      <td>20230331</td>\n",
       "      <td>App</td>\n",
       "      <td>201</td>\n",
       "      <td>唯品会</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>310000</td>\n",
       "      <td>310100</td>\n",
       "      <td>310101</td>\n",
       "      <td>上海市</td>\n",
       "      <td>上海市</td>\n",
       "      <td>黄浦区</td>\n",
       "      <td>20230331</td>\n",
       "      <td>App</td>\n",
       "      <td>3602</td>\n",
       "      <td>京东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>310000</td>\n",
       "      <td>310100</td>\n",
       "      <td>310101</td>\n",
       "      <td>上海市</td>\n",
       "      <td>上海市</td>\n",
       "      <td>黄浦区</td>\n",
       "      <td>20230331</td>\n",
       "      <td>年龄</td>\n",
       "      <td>10.226%</td>\n",
       "      <td>[50~55)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>310000</td>\n",
       "      <td>310100</td>\n",
       "      <td>310101</td>\n",
       "      <td>上海市</td>\n",
       "      <td>上海市</td>\n",
       "      <td>黄浦区</td>\n",
       "      <td>20230331</td>\n",
       "      <td>终端品牌</td>\n",
       "      <td>0.0055%</td>\n",
       "      <td>唐为</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>310000</td>\n",
       "      <td>310100</td>\n",
       "      <td>310101</td>\n",
       "      <td>上海市</td>\n",
       "      <td>上海市</td>\n",
       "      <td>黄浦区</td>\n",
       "      <td>20230331</td>\n",
       "      <td>终端品牌</td>\n",
       "      <td>1.4456%</td>\n",
       "      <td>三星</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>310000</td>\n",
       "      <td>310100</td>\n",
       "      <td>310101</td>\n",
       "      <td>上海市</td>\n",
       "      <td>上海市</td>\n",
       "      <td>黄浦区</td>\n",
       "      <td>20230331</td>\n",
       "      <td>终端品牌</td>\n",
       "      <td>0.0028%</td>\n",
       "      <td>诺基亚</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>100</th>\n",
       "      <td>310000</td>\n",
       "      <td>310100</td>\n",
       "      <td>310101</td>\n",
       "      <td>上海市</td>\n",
       "      <td>上海市</td>\n",
       "      <td>黄浦区</td>\n",
       "      <td>20230331</td>\n",
       "      <td>终端品牌</td>\n",
       "      <td>0.0014%</td>\n",
       "      <td>依偎</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>101</th>\n",
       "      <td>310000</td>\n",
       "      <td>310100</td>\n",
       "      <td>310101</td>\n",
       "      <td>上海市</td>\n",
       "      <td>上海市</td>\n",
       "      <td>黄浦区</td>\n",
       "      <td>20230331</td>\n",
       "      <td>终端品牌</td>\n",
       "      <td>0.0083%</td>\n",
       "      <td>乐视移动</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>102 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     prov_code  city_code  region_code prov_name city_name region_name  \\\n",
       "0       310000     310100       310101       上海市       上海市         黄浦区   \n",
       "1       310000     310100       310101       上海市       上海市         黄浦区   \n",
       "2       310000     310100       310101       上海市       上海市         黄浦区   \n",
       "3       310000     310100       310101       上海市       上海市         黄浦区   \n",
       "4       310000     310100       310101       上海市       上海市         黄浦区   \n",
       "..         ...        ...          ...       ...       ...         ...   \n",
       "97      310000     310100       310101       上海市       上海市         黄浦区   \n",
       "98      310000     310100       310101       上海市       上海市         黄浦区   \n",
       "99      310000     310100       310101       上海市       上海市         黄浦区   \n",
       "100     310000     310100       310101       上海市       上海市         黄浦区   \n",
       "101     310000     310100       310101       上海市       上海市         黄浦区   \n",
       "\n",
       "         date statis_type    value statis_field  \n",
       "0    20230331         App      852           天猫  \n",
       "1    20230331         App    18016          拼多多  \n",
       "2    20230331         App      201          唯品会  \n",
       "3    20230331         App     3602           京东  \n",
       "4    20230331          年龄  10.226%      [50~55)  \n",
       "..        ...         ...      ...          ...  \n",
       "97   20230331        终端品牌  0.0055%           唐为  \n",
       "98   20230331        终端品牌  1.4456%           三星  \n",
       "99   20230331        终端品牌  0.0028%          诺基亚  \n",
       "100  20230331        终端品牌  0.0014%           依偎  \n",
       "101  20230331        终端品牌  0.0083%         乐视移动  \n",
       "\n",
       "[102 rows x 10 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d39929be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['prov_code', 'city_code', 'region_code', 'prov_name', 'city_name',\n",
      "       'region_name', 'date', 'statis_type', 'value', 'statis_field'],\n",
      "      dtype='object')\n",
      "RangeIndex(start=0, stop=102, step=1)\n"
     ]
    }
   ],
   "source": [
    "# 列名\n",
    "print(df.columns)\n",
    "# 索引\n",
    "print(df.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "df2c4b2c",
   "metadata": {},
   "outputs": [],
   "source": [
    "grouped = df.groupby(by=\"statis_type\")\n",
    "\n",
    "for value,group in grouped:\n",
    "    filename=str(value)+'.'+'csv'\n",
    "    try:\n",
    "        f=open(filename,'w')\n",
    "        if f:\n",
    "            #清空文件\n",
    "            f.truncate()\n",
    "            #写入新文件\n",
    "            group.to_csv(filename,sep=',',index=False,mode='w',encoding='utf-8')\n",
    "    except Exception as e:\n",
    "            print(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "35c31018",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pandas.core.groupby.generic.DataFrameGroupBy object at 0x000001E8E08F7430>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72ad748c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
